package cn.szzsi._sys;


import cn.szzsi.common.model.Admanage;
import cn.szzsi.common.model.Adtype;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

//import com.jshop.common.QiniuUtil;

/**
 * 广告管理
 * Created by yuanzheng on 2016/11/30.
 */
public class AdManageController extends Controller{
    private String shopId;
    private Admanage admanage;
    public void list(){
        render("ad_manage_list.html");

    }
    //ajax list
    public void ajaxlist(){
        shopId="-1";
        List<Admanage> lpa = Admanage.dao.find("select * from mall_admanage where uid = ? order by createDate desc",shopId);
        for(Admanage pa:lpa){
            pa.init();
        }
        renderJson(lpa);
    }

    // 添加
    public void add() {
        shopId="-1";
        setAttr("adTypeList", Adtype.dao.find("select * from mall_adtype"));
        setAttr("shopId",shopId);
        render("ad_manage_input.html");
    }

    // 编辑
    public void edit() {
        shopId="-1";
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("admanage", Admanage.dao.findById(id));
        }
        setAttr("shopId",shopId);
        setAttr("adTypeList", Adtype.dao.find("select * from mall_adtype"));
        render("ad_manage_input.html");
    }

    public void save(){
        shopId="-1";
        String title=getPara("admanage.title");
        String adTypeId=getPara("admanage.adTypeId");
        String url=getPara("admanage.url");
        String image=getPara("admanage.image");
        Admanage.saveAdManage(adTypeId,title,image,url,shopId);
        renderJson(Ret.ok("info","添加成功").set("url","list"));

    }

    public void update(){
        Integer id=getParaToInt("admanage.id");
        String title=getPara("admanage.title");
        String adTypeId=getPara("admanage.adTypeId");
        String url=getPara("admanage.url");
        String image=getPara("admanage.image");
        Admanage adManage= Admanage.dao.findById(id);
        Admanage.updateAdManage(adManage,adTypeId,title,image,url);
        renderJson(Ret.ok("info","修改成功").set("url","list"));

    }

    // 删除
    public void delete(){
        String id=getPara("id");
        if(Admanage.dao.deleteById(id)){
            renderJson(Ret.ok());
        }else{
            renderJson(Ret.fail());

        }

    }
}
